/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.viewpagerindicator.slice;

import com.zhpan.indicator.IndicatorComponent;
import com.zhpan.indicator.enums.IndicatorOrientation;
import com.zhpan.indicator.enums.IndicatorSlideMode;
import com.zhpan.indicator.enums.IndicatorStyle;
import com.zhpan.indicator.utils.Log;
import com.zhpan.viewpagerindicator.DrawableIndicator;
import com.zhpan.viewpagerindicator.FigureIndicatorComponent;
import com.zhpan.viewpagerindicator.ResourceTable;
import com.zhpan.viewpagerindicator.provider.PagerProvider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.ComponentState;
import ohos.agp.components.PageSlider;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import java.io.IOException;

/**
 * 测试Demo入口类
 *
 * @since 2021-04-13
 */
public class MainAbilitySlice extends AbilitySlice {
    private float normalWidthS;
    private float checkedWidthS;

    private float normalWidth;
    private float checkedWidth;

    private IndicatorComponent indicator;
    private PageSlider pageSlider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        normalWidthS = AttrHelper.vp2px(16, getContext());
        checkedWidthS = normalWidthS * 1.2f;

        normalWidth = AttrHelper.vp2px(12, getContext());
        checkedWidth = AttrHelper.vp2px(12, getContext());

        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slide);
        pageSlider.setProvider(new PagerProvider());
        indicator = (IndicatorComponent) findComponentById(ResourceTable.Id_ind);

        indicator.setSlideMode(IndicatorSlideMode.SMOOTH);
        indicator.setIndicatorStyle(IndicatorStyle.CIRCLE);
        indicator.setSliderHeight(AttrHelper.vp2px(6, getContext()));
        indicator.setSliderGap(AttrHelper.vp2px(4, getContext()));
        indicator.setSliderWidth(normalWidth, checkedWidth);
        indicator.setOrientation(IndicatorOrientation.INDICATOR_HORIZONTAL);
        indicator.setupWithPageSlide(pageSlider);

        FigureIndicatorComponent figure = (FigureIndicatorComponent) findComponentById(ResourceTable.Id_figure);
        figure.setRadius(AttrHelper.vp2px(20, this));
        figure.setTextSize(AttrHelper.fp2px(16, this));
        figure.setBackgroundColor(new Color(Color.getIntColor("#aa118EEA")));
        figure.setTextColor(new Color(Color.getIntColor("#ffffff")));
        figure.setupWithPageSlide(pageSlider);

        DrawableIndicator drawableIndicator =
            (DrawableIndicator) findComponentById(ResourceTable.Id_drawable_indicator);
        drawableIndicator.setIndicatorSize(50, 50, 50, 50);
        drawableIndicator.setIndicatorDrawable(ResourceTable.Media_heart_empty, ResourceTable.Media_heart_red);
        drawableIndicator.setupWithPageSlide(pageSlider);

        RadioContainer orientationRadio = (RadioContainer) findComponentById(ResourceTable.Id_radio_orientation);
        RadioContainer styleRadio = (RadioContainer) findComponentById(ResourceTable.Id_radio_style);
        RadioContainer modelRadio = (RadioContainer) findComponentById(ResourceTable.Id_radio_model);

        styleRadio.setMarkChangedListener((radioContainer, index) -> {
            switchStyle(index);
        });
        modelRadio.setMarkChangedListener((radioContainer, index) -> {
            switchModel(index);
        });
        orientationRadio.setMarkChangedListener((radioContainer, index) -> {
            switchOrientation(index);
        });
    }

    private void switchOrientation(int index) {
        switch (index) {
            case 0:
                indicator.setOrientation(IndicatorOrientation.INDICATOR_HORIZONTAL);
                break;
            case 1:
                indicator.setOrientation(IndicatorOrientation.INDICATOR_VERTICAL);
                break;
            case 2:
                indicator.setOrientation(IndicatorOrientation.INDICATOR_RTL);
                break;
            default:
                break;
        }
        applyIndicator();
    }

    private void switchStyle(int index) {
        switch (index) {
            case 0:
                indicator.setIndicatorStyle(IndicatorStyle.CIRCLE);
                break;
            case 1:
                indicator.setIndicatorStyle(IndicatorStyle.DASH);
                break;
            case 2:
                indicator.setIndicatorStyle(IndicatorStyle.ROUND_RECT);
                break;
            default:
                break;
        }
        applyIndicator();
    }

    private void switchModel(int index) {
        switch (index) {
            case 0:
                indicator.setSlideMode(IndicatorSlideMode.SMOOTH);
                break;
            case 1:
                indicator.setSlideMode(IndicatorSlideMode.WORM);
                break;
            case 2:
                indicator.setSlideMode(IndicatorSlideMode.COLOR);
                break;
            case 3:
                indicator.setSlideMode(IndicatorSlideMode.SCALE);
                break;
            case 4:
                indicator.setSlideMode(IndicatorSlideMode.NORMAL);
                break;
            default:
                break;
        }
        applyIndicator();
    }

    private void applyIndicator() {
        int slideMode = indicator.getSlideMode();
        if (slideMode == IndicatorSlideMode.SCALE || slideMode == IndicatorSlideMode.NORMAL) {
            indicator.setSliderWidth(normalWidthS, checkedWidthS);
        } else {
            indicator.setSliderWidth(normalWidth, checkedWidth);
        }
        indicator.notifyDataChanged();
    }

    private StateElement createStateElement() {
        PixelMapElement selectElement = createElement(ResourceTable.Media_ic_radio_checked);
        PixelMapElement emptyElement = createElement(ResourceTable.Media_ic_radio_un_checked);

        StateElement stateElement = new StateElement();
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, selectElement);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, emptyElement);
        return stateElement;
    }

    private PixelMapElement createElement(int res) {
        Resource resource = null;
        try {
            resource = getResourceManager().getResource(res);
        } catch (IOException | NotExistException e) {
            Log.e("Main", e.getMessage());
        }
        return new PixelMapElement(resource);
    }

    @Override
    public void onActive() {
        super.onActive();
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slide);
        indicator = (IndicatorComponent) findComponentById(ResourceTable.Id_ind);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
